package com.sproutcat.sample;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SynchronizedObjectLock implements Runnable {

    static SynchronizedObjectLock instence1 = new SynchronizedObjectLock();
    static SynchronizedObjectLock instence2 = new SynchronizedObjectLock();
    // 创建2把锁
    final Object block1 = new Object();
    final Object block2 = new Object();

    @Override
    public void run() {
        // 这个代码块使用的是第一把锁，当他释放后，后面的代码块由于使用的是第二把锁，因此可以马上执行
        synchronized (block1) {
            System.out.println("block1锁,我是线程[" + Thread.currentThread().getName() + "]");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block1锁,["+Thread.currentThread().getName() + "]结束");
        }

        synchronized (block2) {
            System.out.println("block2锁,我是线程[" + Thread.currentThread().getName() + "]");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block2锁,["+Thread.currentThread().getName() + "]结束");
        }
    }

    public synchronized void test1(String txt) {
        System.out.println("===>> 我是线程[" + Thread.currentThread().getName() + "][" + txt + "]");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("<<===[" + Thread.currentThread().getName() + "][" + txt +"]结束");
    }

    public static synchronized void test2(String txt) {
        System.out.println("===>> 我是线程[" + Thread.currentThread().getName() + "][" + txt + "]");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("<<===[" + Thread.currentThread().getName() + "][" + txt +"]结束");
    }

    public  void test3(String txt) {
        synchronized(SynchronizedObjectLock.class) {
            System.out.println("===>> 我是线程[" + Thread.currentThread().getName() + "][" + txt + "]");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("<<===[" + Thread.currentThread().getName() + "][" + txt + "]结束");
        }
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.execute(instence1);
//        executorService.execute(instence1);
//        executorService.execute(() -> instence1.test1("1-1"));
//        executorService.execute(() -> instence1.test1("1-2"));
//        executorService.execute(() -> instence2.test1("1-3"));
//        executorService.execute(() -> instence1.test2("2-1"));
//        executorService.execute(() -> instence1.test2("2-2"));
//        executorService.execute(() -> instence2.test2("2-3"));
        executorService.execute(() -> instence1.test3("3-1"));
        executorService.execute(() -> instence1.test3("3-2"));
        executorService.execute(() -> instence2.test3("3-3"));
        executorService.shutdown();
    }
}
